package com.example.demo.T2.util;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 *
 * @author Lizhong
 * @date 2020/8/18
 */
public class FakeTimer {

    long minMills;
    long maxMills;
    long startMillsSeconds;
    LocalDateTime start;

    double rate;

    ZoneOffset zo = ZoneOffset.UTC;

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");



    public FakeTimer(long minFakeSeconds, long maxFakeSeconds, int minRealSeconds, int maxRealSeconds) {
        if (minFakeSeconds > maxFakeSeconds)
            throw new RuntimeException("minFakeSeconds not rather than maxFakeSeconds ");
        this.minMills = minFakeSeconds;
        this.maxMills = maxFakeSeconds;

        this.start = LocalDateTime.now();
        this.startMillsSeconds = start.toInstant(zo).toEpochMilli();

        int i = RangeRandomUtils.rangeRandom(minRealSeconds, maxRealSeconds);
        rate = (double) i/maxFakeSeconds;

    }


    public long getCurrentFakeSeconds() {
        // 真实运行秒数
        long currentRealMills =  (LocalDateTime.now().toInstant(zo).minusMillis(startMillsSeconds).toEpochMilli());
        LocalDateTime fakeClock = LocalDateTime.ofInstant(Instant.ofEpochMilli ((long)( currentRealMills / rate)),  zo);
        long second = fakeClock.toEpochSecond(zo) ;
        return second;
    }

    public long getCurrentFakeMiutes() {
        // 真实运行秒数
        long currentRealMills =  (LocalDateTime.now().toInstant(zo).minusMillis(startMillsSeconds).toEpochMilli());
        LocalDateTime fakeClock = LocalDateTime.ofInstant(Instant.ofEpochMilli ((long)( currentRealMills / rate)),  zo);
        long second = fakeClock.toEpochSecond(zo) ;
        return second/60;
    }

    public long getCurrentRealSeconds() {
        long currentRealMills =  (LocalDateTime.now().toInstant(zo). minusMillis(startMillsSeconds).toEpochMilli());
        LocalDateTime fakeClock = LocalDateTime.ofInstant(Instant.ofEpochMilli ((long)( currentRealMills )),  zo);
        long second = fakeClock.toEpochSecond(zo) ;
        return second;

    }
    public String getFakeClock() {
        long currentRealMills =  (LocalDateTime.now().toInstant(zo).toEpochMilli()-(startMillsSeconds));
        LocalDateTime fakeClock = LocalDateTime.ofInstant(Instant.ofEpochMilli ((long)( currentRealMills / rate)),  zo);

        String minus = fakeClock.format(formatter);
        return minus;

    }



    public double fake2Real(double  fakeMints) {
        return  (fakeMints * rate);

    }

    public long real2fake(long fakeMints) {
        return (long) (fakeMints / rate);

    }


    public static void main(String[] args) throws InterruptedException {
        FakeTimer fakeTimer = new FakeTimer(0, 60 * 60, 15, 40);
        for (int i = 0; i < 40; i++) {
            // System.out.println(fakeTimer.getCurrentFakeSeconds());
            // System.out.println(fakeTimer.getCurrentRealSeconds());
            int millis = 1000;
            double v = fakeTimer.fake2Real(3000);
            // System.out.println(v);
            // String fakeClock = fakeTimer.VS();
            System.out.println(fakeTimer.getCurrentRealSeconds() + " + " + fakeTimer.getFakeClock());

            Thread.sleep(millis);


        }
    }

    public void reset() {
        this.startMillsSeconds = LocalDateTime.now().toInstant (zo).toEpochMilli();
    }
}
